﻿using SFA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.DataAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Configuration;
using IQKS.AWP.BusinessAccessLayer;

public partial class PayResponse : System.Web.UI.Page
{
    public string ReqPayKeystr = ConfigurationSettings.AppSettings["ReqPayKey"];
    public string strConMerchantID = ConfigurationSettings.AppSettings["MerchantIDINR"];
    public string strConMerchantIDGBP = ConfigurationSettings.AppSettings["MerchantIDGBP"];
    public string strConMerchantIDUSD = ConfigurationSettings.AppSettings["MerchantIDUSD"];
    protected void Page_PreInit(object sender, EventArgs e)
    {
          string strMasterID= Convert.ToString(Session["MasterSession"]);
        
        if (Request.QueryString["PayFromAdmin"] != null)
        {
            Page.MasterPageFile = "~/MasterPages/AdminMaster.master";
           
      
            //Session["CandidateID"] = Request.QueryString["CandidateID"];
            //Session["UserID"] = Request.QueryString["UserID"];
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       

        if (Convert.ToString(Page.MasterPageFile).ToUpper().Contains("/MASTERPAGES/ADMINMASTER.MASTER"))
        {
            hidBacktoLogin.Visible = false;
        }
        else
        {
            hidBacktoLogin.Visible = true;
        }

        string canFullName = Convert.ToString(Session["FullName"]);
        string strPayCurrencyType = Convert.ToString(Session["PayCurrencyType"]);
        lblCandidateName.Text = canFullName;
        if (Session["RedirectedFromOfflinePayment"] != null)
        {
            if (Session["RedirectedFromOfflinePayment"].ToString() == "1")
            {
                string cpAmount = Convert.ToString(Session["Amount"]);
                rwTransaction.Visible = false;
                rwTransactionMsg.Visible = false;
                lblCurrencyType.Text = strPayCurrencyType;
                lblAmount.Text = Convert.ToString(cpAmount);
                lblMessage.Text = "Transaction Successfully Completed.";
            }
        }
        else
        {
            string strMaster = Convert.ToString(Session["MasterSession"]);
            string strCandidateUser = Convert.ToString(Session["CandidateUser"]);
            //if (strMaster == "/MASTERPAGES/ADMINMASTER.MASTER")
            if (strCandidateUser != "")
            {
                hidBacktoLogin.Visible = true;
            }

            else
            {
                hidBacktoLogin.Visible = false;

            }

            PGResponse oPgResp = new PGResponse();
            EncryptionUtil lEncUtil = new EncryptionUtil();
            string respcd = null;
            string respmsg = null;
            string astrResponseData = null;
            string strMerchantId, astrFileName = null;
            string strKey = null;
            string strDigest = null;
            string astrsfaDigest = null;

            string cndID = Convert.ToString(Session["CandidateID"]);
            rwTransactionMsg.Visible = true;
            string cpAmount = Convert.ToString(Session["Amount"]);
            double cAmount = (double)Convert.ToDouble(cpAmount);
            if (Convert.ToString(strPayCurrencyType).ToUpper() == "INDRUPEE")
            {
                strMerchantId = strConMerchantID;
            }
            else if (Convert.ToString(strPayCurrencyType).ToUpper() == "BIRTISHPOUND")
            {
                strMerchantId = strConMerchantIDGBP;
            }
            else if (Convert.ToString(strPayCurrencyType).ToUpper() == "USDOLLAR")
            {
                strMerchantId = strConMerchantIDUSD;
            
            }
            else
            {
                strMerchantId = strConMerchantID;
            }
            
            string KeyFileName = Server.MapPath(ReqPayKeystr);
            astrFileName = KeyFileName + strMerchantId + ".key";
            if (Request.ServerVariables["REQUEST_METHOD"] == "POST")
            {
                astrResponseData = Request.Form["DATA"];
                strDigest = Request.Form["EncryptedData"];
                astrsfaDigest = lEncUtil.getHMAC(astrResponseData, astrFileName, strMerchantId);
                if (strDigest.Equals(astrsfaDigest))
                {
                    oPgResp.getResponse(astrResponseData);
                    respcd = oPgResp.RespCode;
                    respmsg = oPgResp.RespMessage;
                }
            }
            PayResponseBAL objPRBal;

            PayResponseBO objPRBO;
            //todo 
            Guid gudUserID = new Guid(Convert.ToString(Session["CandidateUserID"]));
            string strReturnValue;
            objPRBal = new PayResponseBAL();
            objPRBO = new PayResponseBO();
            objPRBO.SetMethod = "INSERT";
            objPRBO.PaymentID = 0;
            //todo  production live


            //objPRBO.RespCode = Convert.ToInt32(oPgResp.RespCode);
            objPRBO.RespCode = 0;
            objPRBO.CandidateID = Convert.ToInt32(cndID);
            objPRBO.RespMessage = oPgResp.RespMessage;
            objPRBO.TxnId = oPgResp.TxnId;
            objPRBO.EPGTxnId = oPgResp.EPGTxnId;
            objPRBO.AuthIdCode = oPgResp.AuthIdCode;
            objPRBO.RRN = oPgResp.RRN;
            objPRBO.CVRespCode = oPgResp.CVRespCode;
            objPRBO.Cookie = oPgResp.Cookie;
            objPRBO.FDMSScore = oPgResp.FDMSScore;
            objPRBO.FDMSResult = oPgResp.FDMSResult;
            objPRBO.CreatedBy = gudUserID;
            objPRBO.PaymentAmount = cAmount;
            strReturnValue = (string)objPRBal.SetPayResponse(objPRBO);
            if (objPRBO.RespCode == 0)
            {
                if (strReturnValue == "SUCCESS")
                {
                    lblMessage.Text = "Transaction Successfully Completed.";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    rwTransactionMsg.Visible = true;
                }
                else if (strReturnValue == "EXIST")
                {
                    lblMessage.Text = "Transaction Unsuccesfull Try Again."; //EnrollResources.GetMessage(Message.InsertMessage); // 
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    rwTransactionMsg.Visible = false;
                }
                else
                {
                    lblMessage.Text = "Transaction Unsuccesfull Try Again."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    rwTransactionMsg.Visible = false;
                }

            }
            else if (objPRBO.RespCode == 1)
            {
                lblTSMessage.Text = "Transaction Unsuccesfull Try Again.";
                lblTSMessage.ForeColor = System.Drawing.Color.Red;
                rwTransactionMsg.Visible = false;
            }
            else
            {
                lblTSMessage.Text = "Invalid card number. Transaction Unsuccesfull Try Again.";
                lblTSMessage.ForeColor = System.Drawing.Color.Red;
                rwTransactionMsg.Visible = false;
            }

            rwTransaction.Visible = false;
            lblTxnId.Text = oPgResp.TxnId;
            lblCandidateName.Text = canFullName;
            lblCurrencyType.Text = strPayCurrencyType;
            lblAmount.Text = Convert.ToString(cAmount);
       
        }
    }
}

